{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "39872d5c",
   "metadata": {},
   "source": [
    "# Javascript to Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "11a37fd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'function $(a) { [python code] }'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import js2py\n",
    "f = js2py.eval_js( \"function $(a) {return a + arguments[1]}\" )\n",
    "f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7d8504e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b2c95ff9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "from js2py.pyjs import *\n",
      "# setting scope\n",
      "var = Scope( JS_BUILTINS )\n",
      "set_global_object(var)\n",
      "\n",
      "# Code follows:\n",
      "var.registers(['$'])\n",
      "@Js\n",
      "def PyJsHoistedNonPyName(a, this, arguments, var=var):\n",
      "    var = Scope({'a':a, 'this':this, 'arguments':arguments}, var)\n",
      "    var.registers(['a'])\n",
      "    return (var.get('a')+var.get('arguments').get('1'))\n",
      "PyJsHoistedNonPyName.func_name = '$'\n",
      "var.put('$', PyJsHoistedNonPyName)\n",
      "pass\n",
      "pass\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(js2py.translate_js( \"function $(a) {return a + arguments[1]}\" ))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7456b04d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from js2py.pyjs import *\n",
    "# setting scope\n",
    "var = Scope( JS_BUILTINS )\n",
    "set_global_object(var)\n",
    "\n",
    "# Code follows:\n",
    "var.registers(['$'])\n",
    "@Js\n",
    "def PyJsHoistedNonPyName(a, this, arguments, var=var):\n",
    "    var = Scope({'a':a, 'this':this, 'arguments':arguments}, var)\n",
    "    var.registers(['a'])\n",
    "    return (var.get('a')+var.get('arguments').get('1'))\n",
    "PyJsHoistedNonPyName.func_name = '$'\n",
    "var.put('$', PyJsHoistedNonPyName)\n",
    "pass\n",
    "pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6085997f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PyJsHoistedNonPyName(1, 2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "52aab6b3",
   "metadata": {},
   "outputs": [
    {
     "ename": "JsException",
     "evalue": "ReferenceError: print is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mJsException\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-9-f013334b0e77>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0moutput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjs2py\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0meval_js\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'function output() {let seed = -734744700160476640; print(\"Copy this output - \" + seed.high, seed.low);}'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0moutput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\base.py\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *args)\u001b[0m\n\u001b[0;32m   1201\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;34m'_prop_of'\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__dict__\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1202\u001b[0m             \u001b[0mparent\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmeth\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__dict__\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'_prop_of'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1203\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mto_python\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparent\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_obj\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcallprop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmeth\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1204\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mto_python\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_obj\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1205\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\base.py\u001b[0m in \u001b[0;36mcallprop\u001b[1;34m(self, prop, *args)\u001b[0m\n\u001b[0;32m    994\u001b[0m                             '%s is not a function (tried calling property %s of %s)' % (\n\u001b[0;32m    995\u001b[0m                             cand.typeof(), repr(prop), repr(self.Class)))\n\u001b[1;32m--> 996\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mcand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcall\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    997\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    998\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mto_python\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\base.py\u001b[0m in \u001b[0;36mcall\u001b[1;34m(self, this, args)\u001b[0m\n\u001b[0;32m   1462\u001b[0m         \u001b[0margs\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[0mthis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0marguments\u001b[0m  \u001b[1;31m#append extra params to the arg list\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1463\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1464\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mJs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcode\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1465\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mNotImplementedError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1466\u001b[0m             \u001b[1;32mraise\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\host\\jseval.py\u001b[0m in \u001b[0;36mPyJsHoisted_output_\u001b[1;34m(this, arguments, var)\u001b[0m\n",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\base.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, prop, throw)\u001b[0m\n\u001b[0;32m   1151\u001b[0m             \u001b[0mcand\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mown\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1152\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mcand\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1153\u001b[1;33m                 \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprototype\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mprop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mthrow\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1154\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mcand\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1155\u001b[0m         \u001b[1;31m# slow, global scope\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\users\\zhuj\\anaconda3\\envs\\basicresearch\\lib\\site-packages\\js2py\\base.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, prop, throw)\u001b[0m\n\u001b[0;32m   1156\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mprop\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mown\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1157\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mthrow\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1158\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mMakeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ReferenceError'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'%s is not defined'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mprop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1159\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mundefined\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1160\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mPyJs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mprop\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mJsException\u001b[0m: ReferenceError: print is not defined"
     ]
    }
   ],
   "source": [
    "hasCode = js2py.eval_js('function hashCode(s) {let h = 0; for (let i = 0; i < s.length; i++) {h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;} return h;}')\n",
    "# getSeed = js2py.eval_js('function getSeed(s) {let x = BigInt(hashCode(s)); if (/^[\\-\\+0-9]*$/.test(s)) {try {let y = BigInt(s); if (BigInt.asIntN(64, y) !== y) {throw RangeError();} x = y;} catch (err) {}} return { high: x >> 32n, low: BigInt.asIntN(32, x) };}')\n",
    "output = js2py.eval_js('function output() {let seed = -734744700160476640; print(\"Copy this output - \" + seed.high, seed.low);}')\n",
    "\n",
    "output()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bb344ac6",
   "metadata": {},
   "outputs": [],
   "source": [
    "hasCode = js2py.eval_js('function hashCode(s) {let h = 0; for (let i = 0; i < s.length; i++) {h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;} return h;}')\n",
    "getSeed = js2py.eval_js('function getSeed(s) {let x = BigInt(hashCode(s)); if (/^[\\-\\+0-9]*$/.test(s)) {try {let y = BigInt(s); if (BigInt.asIntN(64, y) !== y) {throw RangeError();} x = y;} catch (err) {}} return { high: x >> 32n, low: BigInt.asIntN(32, x) };}')\n",
    "output = js2py.eval_js('function output() {let seed = -734744700160476640; print(\"Copy this output - \" + seed.high, seed.low);}')\n",
    "\n",
    "output()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
